package com.gxar.marketdelivery.domain.repo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gxar.marketdelivery.domain.enums.DeliveryTarget;
import com.gxar.marketdelivery.infra.dao.entity.DeliveryMaterialEntity;
import com.gxar.marketdelivery.infra.dao.mapper.DeliveryMaterialMapper;
import com.gxar.marketdelivery.interfaces.web.result.IResponseStatusMsg;
import com.gxar.marketdelivery.shared.consts.DBConsts;
import com.gxar.marketdelivery.shared.utils.AssertUtils;
import com.gxar.marketdelivery.shared.utils.CollectionUtils;
import com.gxar.marketdelivery.shared.utils.DBUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author nice
 * @version 1.0
 * @created 2023/7/21.
 */
@Service
public class DeliveryMaterialRepository {

    @Autowired
    protected DeliveryMaterialMapper mapper;

    public void create(DeliveryMaterialEntity entity) {
        int ret = mapper.insert(entity);
        AssertUtils.isTrue(DBUtils.isSuccess(ret), IResponseStatusMsg.APIEnum.DB_ERROR, "新增投放物料失败");
    }

    public void updateNotNullById(DeliveryMaterialEntity entity) {
        int ret = mapper.updateById(entity);
        AssertUtils.isTrue(DBUtils.isSuccess(ret), IResponseStatusMsg.APIEnum.DB_ERROR, "更新失败");
    }

    public Integer getCount(Long schemaId) {
        QueryWrapper<DeliveryMaterialEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryMaterialEntity.FIELD_ID, schemaId);
        queryWrapper.eq(DeliveryMaterialEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        Long cnt = mapper.selectCount(queryWrapper);
        return cnt == null ? 0 : cnt.intValue();
    }

    public void delete(DeliveryTarget deliveryTarget, Long targetId) {
        UpdateWrapper<DeliveryMaterialEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(DeliveryMaterialEntity.FIELD_STATUS, DBConsts.STATUS_UNAVAILABLE);
        updateWrapper.eq(DeliveryMaterialEntity.FIELD_DELIVERY_TARGET, deliveryTarget.getCode());
        updateWrapper.eq(DeliveryMaterialEntity.FIELD_TARGET_ID, targetId);
        updateWrapper.eq(DeliveryMaterialEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        mapper.update(getByTarget(deliveryTarget, targetId), updateWrapper);
    }

    public DeliveryMaterialEntity getByTarget(DeliveryTarget materialTarget, Long targetId) {
        QueryWrapper<DeliveryMaterialEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryMaterialEntity.FIELD_DELIVERY_TARGET, materialTarget.getCode());
        queryWrapper.eq(DeliveryMaterialEntity.FIELD_TARGET_ID, targetId);
        queryWrapper.eq(DeliveryMaterialEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        return mapper.selectOne(queryWrapper);
    }

    public Map<Long, List<DeliveryMaterialEntity>> multiGet(DeliveryTarget materialTarget, List<Long> targetIds) {
        QueryWrapper<DeliveryMaterialEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryMaterialEntity.FIELD_DELIVERY_TARGET, materialTarget.getCode());
        queryWrapper.in(DeliveryMaterialEntity.FIELD_TARGET_ID, targetIds);
        queryWrapper.eq(DeliveryMaterialEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        List<DeliveryMaterialEntity> list = mapper.selectList(queryWrapper);
        return CollectionUtils.toListMap(list, DeliveryMaterialEntity::getTargetId);
    }

    public List<DeliveryMaterialEntity> getListByIds(List<Long> ids) {
        QueryWrapper<DeliveryMaterialEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(DeliveryMaterialEntity.FIELD_ID, ids);
        queryWrapper.eq(DeliveryMaterialEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        return mapper.selectList(queryWrapper);
    }

    public DeliveryMaterialEntity getEntity(Long materialId) {
        QueryWrapper<DeliveryMaterialEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryMaterialEntity.FIELD_ID, materialId);
        queryWrapper.eq(DeliveryMaterialEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        return mapper.selectOne(queryWrapper);
    }
}
